package com.studentmanagement.ui;

import com.studentmanagement.model.Student;
import com.studentmanagement.service.StudentService;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.List;

public class StudentManagementPanel extends JPanel {
    private StudentService studentService = new StudentService();
    private JTextField searchNameField;
    private JTextField searchIdField;
    private JTextField searchDeptField;
    private JButton searchButton;
    private JButton addButton;
    private JButton editButton;
    private JButton deleteButton;
    private JTable studentTable;
    private DefaultTableModel tableModel;

    public StudentManagementPanel() {
        initComponents();
        setupLayout();
        loadStudentData(null, null, null);
    }

    private void initComponents() {
        searchNameField = new JTextField(10);
        searchIdField = new JTextField(10);
        searchDeptField = new JTextField(10);
        searchButton = new JButton("查询");
        addButton = new JButton("新增");
        editButton = new JButton("编辑");
        deleteButton = new JButton("删除");

        String[] columnNames = {"ID", "学号", "姓名", "性别", "院系", "专业", "联系电话", "邮箱"};
        tableModel = new DefaultTableModel(columnNames, 0) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        studentTable = new JTable(tableModel);
        studentTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        studentTable.setFont(new Font("微软雅黑", Font.PLAIN, 13));
        studentTable.getTableHeader().setFont(new Font("微软雅黑", Font.BOLD, 13));
    }

    private void setupLayout() {
        setLayout(new BorderLayout(10, 10));
        setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        // 搜索栏
        JPanel searchPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        searchPanel.add(new JLabel("姓名:"));
        searchPanel.add(searchNameField);
        searchPanel.add(new JLabel("学号:"));
        searchPanel.add(searchIdField);
        searchPanel.add(new JLabel("院系:"));
        searchPanel.add(searchDeptField);
        searchPanel.add(searchButton);

        // 按钮栏
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        buttonPanel.add(addButton);
        buttonPanel.add(editButton);
        buttonPanel.add(deleteButton);

        add(searchPanel, BorderLayout.NORTH);
        add(new JScrollPane(studentTable), BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);

        // 事件绑定
        searchButton.addActionListener(this::onSearch);
        addButton.addActionListener(this::onAdd);
        editButton.addActionListener(this::onEdit);
        deleteButton.addActionListener(this::onDelete);
    }

    private void loadStudentData(String name, String studentId, String department) {
        tableModel.setRowCount(0);
        List<Student> students = studentService.searchStudents(name, department, null);
        for (Student s : students) {
            tableModel.addRow(new Object[]{
                    s.getId(),
                    s.getStudentId(),
                    s.getName(),
                    s.getGender(),
                    s.getDepartment(),
                    s.getMajor(),
                    s.getContactNumber(),
                    s.getEmail()
            });
        }
    }

    private void onSearch(ActionEvent e) {
        loadStudentData(searchNameField.getText(), searchIdField.getText(), searchDeptField.getText());
    }

    private void onAdd(ActionEvent e) {
        StudentEditDialog dialog = new StudentEditDialog(null);
        dialog.setVisible(true);
        if (dialog.isSaved()) {
            loadStudentData(null, null, null);
        }
    }

    private void onEdit(ActionEvent e) {
        int row = studentTable.getSelectedRow();
        if (row == -1) {
            JOptionPane.showMessageDialog(this, "请先选择要编辑的学生。", "提示", JOptionPane.WARNING_MESSAGE);
            return;
        }
        Integer id = (Integer) tableModel.getValueAt(row, 0);
        Student student = studentService.getStudentById(id);
        StudentEditDialog dialog = new StudentEditDialog(student);
        dialog.setVisible(true);
        if (dialog.isSaved()) {
            loadStudentData(null, null, null);
        }
    }

    private void onDelete(ActionEvent e) {
        int row = studentTable.getSelectedRow();
        if (row == -1) {
            JOptionPane.showMessageDialog(this, "请先选择要删除的学生。", "提示", JOptionPane.WARNING_MESSAGE);
            return;
        }
        int confirm = JOptionPane.showConfirmDialog(this, "确定要删除该学生吗？", "确认删除", JOptionPane.YES_NO_OPTION);
        if (confirm == JOptionPane.YES_OPTION) {
            Integer id = (Integer) tableModel.getValueAt(row, 0);
            boolean success = studentService.deleteStudent(id);
            if (success) {
                JOptionPane.showMessageDialog(this, "删除成功！", "提示", JOptionPane.INFORMATION_MESSAGE);
                loadStudentData(null, null, null);
            } else {
                JOptionPane.showMessageDialog(this, "删除失败！", "错误", JOptionPane.ERROR_MESSAGE);
            }
        }
    }
} 